package com.nuoniu.sibanyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.entity.SysDictData;
import com.nuoniu.sibanyun.entity.dto.DictDataSaveDto;
import com.nuoniu.sibanyun.entity.dto.DictDataSearchDto;
import com.nuoniu.sibanyun.mapper.SysDictDataMapper;
import com.nuoniu.sibanyun.service.ISysDictDataService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.apache.xmlbeans.impl.xb.xmlconfig.Usertypeconfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-05-25
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Override
    public Page<SysDictData> page(DictDataSearchDto dictDataSearchDto) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        if(null != dictDataSearchDto.getDictTypeId() && 0 != dictDataSearchDto.getDictTypeId()){
            queryWrapper.eq("dict_type_id", dictDataSearchDto.getDictTypeId());
        }

        queryWrapper.ne("status",1);
        queryWrapper.orderByAsc("show_sort");
        Page<SysDictData> page = page(dictDataSearchDto.getPage(),queryWrapper);
        return page;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean saveOrUpdate(DictDataSaveDto dictDataSaveDto, HttpServletRequest request) {
        UserInfo userInfo = UserThreadLocal.get();
        SysDictData dictData = new SysDictData();
        BeanUtils.copyProperties(dictDataSaveDto, dictData);
        dictData.setCompanyId(userInfo.getCompanyId());
        if(userInfo.getUserName().equals("system")) {
            dictData.setIsSys(true);
        }else {
            dictData.setIsSys(false);
        }
        this.saveOrUpdate(dictData);
        return true;
    }

    @Override
    public boolean updateStatus(Long id, Integer status, HttpServletRequest request) {
        LocalDateTime now = LocalDateTime.now();
        SysDictData sysDictData = new SysDictData();
        sysDictData.setId(id);
        sysDictData.setStatus(status);
        sysDictData.setUpdateDate(now);
        boolean b = updateById(sysDictData);
        return b;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean delete(Long id, HttpServletRequest request) {
        if(id == null) {
            throw new NuoNiuException("参数错误");
        }
        boolean haveChildDictData = isHaveChildDictData(id);
        if(haveChildDictData) {
            throw new NuoNiuException("删除失败，请先删除子节点字典数据");
        }
        SysDictData sysDictData = getById(id);
        if(null == sysDictData){
            throw new NuoNiuException("删除失败，找不到该记录");
        }

        boolean b = removeById(id);
        if(!b) {
            throw new NuoNiuException("删除失败");
        }
        return true;
    }

    @Override
    public boolean isHaveDictData(Long id) {
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_type_id", id);
        int count = count(queryWrapper);
        return count > 0;
    }

    @Override
    public boolean isHaveChildDictData(Long parentId) {
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_type_id", parentId);
        int count = count(queryWrapper);
        return count > 0;
    }

    @Override
    public List<SysDictData> getDictDataAll() {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.ne("status",1);

        return list(wrapper);
    }

    @Override
    public List<DictDataSaveDto> getByType(Long typeId) {
        return baseMapper.getByType(typeId);
    }
}
